

$(document).ready(function() {
	$("#contractValSel").click(addCost);
	$("#contractValClear").click(clearCost);
	
	var prjObjId = $("#prjObjId").val();
	$.getJSON('getOpportunityViewData.htm?prjObjId=' + prjObjId, function(data) {
		
		setFormData("ff", data);
		
		//alert($("#hiddenAddNotes").val());
	});
	
});
function clearCost() {
	$('#contractVal').validatebox('enableValidation');
	var opt = document.getElementById('contractValBreakup');
	opt.options.length = 0;

}

function addCost() {
	var conVal = $("#contractVal").val();
	var cur = $("#contractValCur").combobox('getValue');
	if (conVal == "") {
		showMessager('Invalid input', 'Please input currency value!');
		return false;
	} else if (cur == "") {
		showMessager('Invalid input', 'Please select currency!');
		return false;
	}
	var curOpt = conVal + " " + cur;
	var opt = document.getElementById('contractValBreakup');
	opt.options[opt.options.length] = new Option(curOpt, curOpt);
	$("#contractVal").val("");
	$('#contractVal').numberbox('setValue', '');
	$('#contractVal').validatebox('disableValidation');
}
function getFieldObject(id, value) {
	return {
		"id" : id,
		"value" : value
	};
}



function saveOpportunity() {

	if (!$("#ff").form('validate')) {
		$.messager.show({
			title : 'Alert',
			msg : 'Required fields can not be left blank.',
			showType : 'show'
		});
		return false;
	}
	var data = getFormData("ff");
	var costList = document.getElementById('contractValBreakup');

	var costAry = new Array();
	for ( var i = 0; i < costList.options.length; i++) {
		var d = costList.options[i].value;
		var dAry = d.split(" ");
		costAry.push({
			"value" : dAry[0],
			"currency" : dAry[1]
		});
	}
	if (costAry.length > 0)
		data["contractVal"] = costAry;

	data["userId"] = $("#userId").val();
	data["projectType"] = {
		"id" : $("#projectType").val(),
		"value" : $("#projectType").val()
	};
	data["market"] = {
		"id" : $("#market").val(),
		"value" : $("#market").val()
	};
	data["portfolio"] = {
		"id" : $("#portfolio").val(),
		"value" : $("#portfolio").val()
	};
	data["portfolioName"] = {
			"id" : $("#portfolioName").val(),
			"value" : $("#portfolioName").val()
		};


	$.postJSON("saveOpportunity.htm", data, function(result) {
		if (result.success) {
			showMessager("Info", "Opportunity Saved");
			window.close();
		} else {
			showMessager("Exception", result.message);
		}
	}, function(xhr, ajaxOptions, thrownError) {
		showMessager("Exception", thrownError);
	});

}
function resetForm() {
	$("#ff").reset();
}